package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.mapper.MedicalHistoryMapper;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.InstitutionMedication;
import com.wty.cmims.pojo.MedicalHistory;
import com.wty.cmims.pojo.Medication;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.InstitutionMedicationService;
import com.wty.cmims.service.MedicalHistoryService;
import com.wty.cmims.service.MedicationService;
import com.wty.cmims.mapper.MedicationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【medication】的数据库操作Service实现
* @createDate 2024-11-14 23:00:15
*/
@Service
public class MedicationServiceImpl extends ServiceImpl<MedicationMapper, Medication>
    implements MedicationService{
    @Autowired
    private MedicationMapper medicationMapper;
    @Autowired
    private MedicalHistoryMapper medicalHistoryMapper;
    @Override
    public ResponseResult<Map<String, Object>> medicationFind(Map<String, Object> paramsMap) {
        // 分页
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        // 如果传了病历ID，则查询该医疗机构下所有的药品
        if (paramsMap.get("history_id") != null) {
            LambdaQueryWrapper<MedicalHistory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MedicalHistory::getId, paramsMap.get("history_id"));
            MedicalHistory medicalHistory = medicalHistoryMapper.selectOne(wrapper);
            paramsMap.put("institution_id", medicalHistory.getInstitutionId());
        }
        List<Map<String, Object>> medications = medicationMapper.findMedications(paramsMap);
        Integer total = medicationMapper.findMedicationsNum(paramsMap);
        if (medications != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询药品信息成功");
            responseResult.setDatas(medications);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询药品信息失败");
        }
        return responseResult;
    }

    @Override
    @Transactional
    public ResponseResult<Void> medicationAdd(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        if (Boolean.parseBoolean(paramsMap.get("is_otc").toString())){
            paramsMap.put("is_otc","1");
        } else {
            paramsMap.put("is_otc","0");
        }
        Medication medication = new Medication();
        medication.setName((String) paramsMap.get("name"));
        medication.setIndications((String) paramsMap.get("indications"));
        medication.setPrice(BigDecimal.valueOf(Double.parseDouble((String) paramsMap.get("price"))));
        medication.setSpecification((String) paramsMap.get("specification"));
        medication.setUnit((String) paramsMap.get("unit"));
        medication.setDosage((String) paramsMap.get("dosage"));
        medication.setPrecautions((String) paramsMap.get("precautions"));
        medication.setAdverseEffects((String) paramsMap.get("adverse_effects"));
        medication.setExpirationDate((String) paramsMap.get("expiration_date"));
        medication.setIsOtc(Integer.valueOf((String) paramsMap.get("is_otc")));
        medication.setManufacturer((String) paramsMap.get("manufacturer"));
        medication.setApprovalNumber((String) paramsMap.get("approval_number"));
        medication.setMedicationImg((String) paramsMap.get("medication_img"));
        boolean save = this.save(medication);
//        InstitutionMedication institutionMedication = new InstitutionMedication();
//        institutionMedication.setInstitutionId((Integer) paramsMap.get("institution_id"));
//        institutionMedication.setMedicationId(medication.getId());
//        institutionMedication.setStock((Integer) paramsMap.get("stock"));
//        boolean saveIM = institutionMedicationService.save(institutionMedication);
        if (save) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("药品添加成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("药品添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> medicationUpdate(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        if (Boolean.parseBoolean(paramsMap.get("is_otc").toString())){
            paramsMap.put("is_otc","1");
        } else {
            paramsMap.put("is_otc","0");
        }
        Integer rows = medicationMapper.updateMedication(paramsMap);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("药品信息更新成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("药品信息更新失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> medicationDelete(Integer medicationId) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<Medication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Medication::getId,medicationId);
        wrapper.set(Medication::getIsDel,1);
        boolean delete = this.update(wrapper);
        if (delete) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("药品删除成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("药品删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> medicationDropDownFind(Integer institutionId) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> medications = medicationMapper.findMedicationsDropDown(institutionId);
        if (medications != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询药品下拉框成功");
            responseResult.setDatas(medications);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询药品下拉框失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> medicationDropDownWithoutFind(Integer institutionId) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> medications = medicationMapper.findMedicationsDropDownWithout(institutionId);
        if (medications != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询药品下拉框成功");
            responseResult.setDatas(medications);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询药品下拉框失败");
        }
        return responseResult;
    }
}




